home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 126-150 / disk_136 / bison / derives.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  2KB  |  114 lines

  1. /* Match rules with nonterminals for bison,
  2.    Copyright (C) 1984 Bob Corbett and Free Software Foundation, Inc.
  3.  
  4. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the BISON General Public License for full details.
  9.  
  10. Everyone is granted permission to copy, modify and redistribute BISON,
  11. but only under the conditions described in the BISON General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with BISON so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.  
  17.  In other words, you are welcome to use, share and improve this program.
  18.  You are forbidden to forbid anyone else to use, share and improve
  19.  what you give them.   Help stamp out software-hoarding!  */
  20.  
  21. /* set_derives finds, for each variable (nonterminal), which rules can derive it.
  22.    It sets up the value of derives so that
  23.    derives[i - ntokens] points to a vector of rule numbers, terminated with a zero.  */
  24.  
  25. #include <stdio.h>
  26. #include "new.h"
  27. #include "types.h"
  28. #include "gram.h"
  29.  
  30.  
  31. short **derives;
  32.  
  33.  
  34. set_derives()
  35. {
  36.   register int i;
  37.   register int lhs;
  38.   register shorts *p;
  39.   register short *q;
  40.   register shorts **dset;
  41.   register shorts *delts;
  42.  
  43.   dset = NEW2(nvars, shorts *) - ntokens;
  44.   delts = NEW2(nrules + 1, shorts);
  45.  
  46.   p = delts;
  47.   for (i = nrules; i > 0; i--)
  48.     {
  49.       lhs = rlhs[i];
  50.       p->next = dset[lhs];
  51.       p->value = i;
  52.       dset[lhs] = p;
  53.       p++;
  54.     }
  55.  
  56.   derives = NEW2(nvars, short *) - ntokens;
  57.   q = NEW2(nvars + nrules, short);
  58.  
  59.   for (i = ntokens; i < nsyms; i++)
  60.     {
  61.       derives[i] = q;
  62.       p = dset[i];
  63.       while (p)
  64.     {
  65.       *q++ = p->value;
  66.       p = p->next;
  67.     }
  68.       *q++ = -1;
  69.     }
  70.  
  71. #ifdef    DEBUG
  72.   print_derives();
  73. #endif
  74.  
  75.   FREE(dset + ntokens);
  76.   FREE(delts);
  77. }
  78.  
  79.  
  80. free_derives()
  81. {
  82.   FREE(derives[ntokens]);
  83.   FREE(derives + ntokens);
  84. }
  85.  
  86.  
  87.  
  88. #ifdef    DEBUG
  89.  
  90. print_derives()
  91. {
  92.   register int i;
  93.   register short *sp;
  94.  
  95.   extern char **tags;
  96.  
  97.   printf("\n\n\nDERIVES\n\n");
  98.  
  99.   for (i = ntokens; i < nsyms; i++)
  100.     {
  101.       printf("%s derives", tags[i]);
  102.       for (sp = derives[i]; *sp > 0; sp++)
  103.     {
  104.       printf("  %d", *sp);
  105.     }
  106.       putchar('\n');
  107.     }
  108.  
  109.   putchar('\n');
  110. }
  111.  
  112. #endif
  113.  
  114.